package com.link.base.base.apilog.controller;


import com.link.base.base.apilog.model.ApiLogLine;
import com.link.base.base.apilog.service.ApiLogLineService;
import com.link.core.basic.controller.BasicController;
import com.link.core.basic.service.BasicService;
import com.link.core.util.DateUtil;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * api接口调用日志记录行
 * @author 许博文
 * @date 2019/12/25
 */
@Controller
@RequestMapping("/portal/apiLogLine")
public class ApiLogLineController extends BasicController<ApiLogLine> {

    @Resource
    private ApiLogLineService apiLogLineService;

    private static final Logger logger = LogManager.getLogger(ApiLogLineController.class);

    @Override
    public BasicService<ApiLogLine> getBasicService() throws Exception {
        return apiLogLineService;
    }

    /**
     * DESC: 查询总数+成功数
     *
     * @param:
     * @return:
     * @auther: bowen.xu
     * @date: 2018/10/11 11:02 AM
     */
    @RequestMapping(value = "queryTotalAndSuccess", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> queryTotalAndSuccess(ApiLogLine model, HttpServletRequest request,
                                                    HttpServletResponse response) {
        Map<String, Object> result = new HashMap<String, Object>();
        try {
            String endTime = model.getEndTime();
            Date date = DateUtil.stringToDate(endTime, "yyyy-MM-dd");
            date = DateUtil.addDayToDate(date, 1);
            endTime = DateUtil.dateToStr(date, "yyyy-MM-dd");
            model.setEndTime(endTime);

            Map<String, Long> totalList = apiLogLineService.queryTotalAndSuccess(model);
            model.setProcessStatus("Success");
            Map<String, Long> successList = apiLogLineService.queryTotalAndSuccess(model);
            result.put("totalList", totalList);
            result.put("successList", successList);
            result.put("success", true);
        } catch (Exception e) {
            result.put("success", false);
            result.put("result", e.getMessage());
        }
        return result;
    }


    /**
     * DESC: 总数 (条形图或者趋势图)（指定时间段，以月份为度量单位）
     *
     * @param:
     * @return:
     * @auther: bowen.xu
     * @date: 2018/10/11 11:02 AM
     */
    @RequestMapping(value = "queryNumTopN", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> queryNumTopN(@RequestBody ApiLogLine model, HttpServletRequest request,
                                            HttpServletResponse response) {
        Map<String, Object> result = new HashMap<String, Object>();
        try {
            List<ApiLogLine> list = apiLogLineService.queryNumTopN(model);
            result.put("rows", list);
            result.put("success", true);
        } catch (Exception e) {
            result.put("success", false);
            result.put("result", e.getMessage());
        }
        return result;
    }

    /**
     * DESC: 总数 (条形图或者趋势图)（指定时间段，以天为度量单位）
     *
     * @param:
     * @return:
     * @auther: bowen.xu
     * @date: 2018/10/11 11:02 AM
     */
    @RequestMapping(value = "queryTotalNum", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> queryTotalNum(ApiLogLine model, HttpServletRequest request,
                                             HttpServletResponse response) {
        Map<String, Object> result = new HashMap<String, Object>();
        try {
            List<ApiLogLine> list = apiLogLineService.queryTotalNum(model);
            result.put("rows", list);
            result.put("success", true);
        } catch (Exception e) {
            result.put("success", false);
            result.put("result", e.getMessage());
        }
        return result;
    }

    /**
     * DESC: 错误  top10 (条形图）（可指定时间段，以月份为单位）
     *
     * @param:
     * @return:
     * @auther: bowen.xu
     * @date: 2018/10/11 11:02 AM
     */
    @RequestMapping(value = "queryFailureTopN", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> queryFailureTopN(ApiLogLine model, HttpServletRequest request,
                                                HttpServletResponse response) {
        Map<String, Object> result = new HashMap<String, Object>();
        try {
            List<ApiLogLine> list = apiLogLineService.queryFailureTopN(model);
            result.put("rows", list);
            result.put("success", true);
        } catch (Exception e) {
            result.put("success", false);
            result.put("result", e.getMessage());
        }
        return result;
    }

    /**
     * DESC:
     *
     * @param: 错误  总数 (条形图或者趋势图) （指定时间段，以天为度量单位）
     * @return:
     * @auther: bowen.xu
     * @date: 2018/10/11 11:03 AM
     */
    @RequestMapping(value = "queryFailureNum", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> queryFailureNum(ApiLogLine model, HttpServletRequest request,
                                               HttpServletResponse response) {
        Map<String, Object> result = new HashMap<String, Object>();
        try {
            List<ApiLogLine> list = apiLogLineService.queryFailureNum(model);
            result.put("rows", list);
            result.put("success", true);
        } catch (Exception e) {
            result.put("success", false);
            result.put("result", e.getMessage());
        }
        return result;
    }

    /**
     * DESC: 错误接口，正确接口比例 （饼图）（以月份为单位）
     *
     * @param:
     * @return:
     * @auther: bowen.xu
     * @date: 2018/10/11 11:03 AM
     */
    @RequestMapping(value = "queryRatio", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> queryRatio(ApiLogLine model, HttpServletRequest request,
                                          HttpServletResponse response) {
        Map<String, Object> result = new HashMap<String, Object>();
        try {
            ApiLogLine apiLogLine = apiLogLineService.queryRatio(model);
            result.put("result", apiLogLine);
            result.put("success", true);
        } catch (Exception e) {
            result.put("success", false);
            result.put("result", e.getMessage());
        }
        return result;
    }

    /**
     * DESC:
     *
     * @param: 查询最上方三个单值报表
     * @return:
     * @auther: bowen.xu
     * @date: 2018/10/11 11:03 AM
     */
    @RequestMapping(value = "queryTopReport", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> queryTopReport(ApiLogLine model, HttpServletRequest request,
                                              HttpServletResponse response) {
        Map<String, Object> result = new HashMap<String, Object>();
        try {
            Long headNum = 0L;
            ApiLogLine apiLogLine2 = apiLogLineService.queryTotalHeadNum(model);
            if (apiLogLine2 != null) {
                headNum = apiLogLine2.getTotalNum();
            }
            Long todayNum = 0L;
            Double todaySuccessRatio = 0.0;

            List<ApiLogLine> apiLogLines = apiLogLineService.queryTotalNum(model);
            if (apiLogLines.size() > 0) {
                for (ApiLogLine item : apiLogLines) {
                    todayNum += item.getTotalNum();
                }
            }
            ApiLogLine apiLogLine1 = apiLogLineService.queryRatio(model);
            Long successNum = apiLogLine1.getSuccessNum();
            Long totalNum = apiLogLine1.getTotalNum();
            if (totalNum == 0) {
                todaySuccessRatio = 100.0;
            } else {
                double f1 = BigDecimal.valueOf((float) successNum / totalNum).setScale(4, BigDecimal.ROUND_HALF_UP).doubleValue();
                todaySuccessRatio = f1 * 100 ;
            }

            result.put("headNum", headNum);
            result.put("todayNum", todayNum);
            result.put("todaySuccessNum", todaySuccessRatio);
            result.put("success", true);
        } catch (Exception e) {
            result.put("success", false);
            result.put("result", e.getMessage());
            logger.error("ApiLogLineController捕获异常: ",e);
        }
        return result;
    }

    /**
     * @param
     * @return
     * @throws Exception 报错
     * @auther HuangLJ
     * @date 2018/10/15 4:07 PM
     * @comments client接口报错前十名
     */
    @RequestMapping(value = "queryClientFailureTopN", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> queryClientFailureTopN(@RequestBody ApiLogLine model, HttpServletRequest request,
                                                      HttpServletResponse response) {
        Map<String, Object> result = new HashMap<String, Object>();
        try {
            List<ApiLogLine> list = apiLogLineService.queryClientFailureTopN(model);
            result.put("rows", list);
            result.put("success", true);
        } catch (Exception e) {
            result.put("success", false);
            result.put("result", e.getMessage());
        }
        return result;
    }
}
